<?php
require_once (APPPATH.'libraries/paginationobject.php');
require_once (APPPATH.'views/components/page.php');

/**
 * Product Type Controller
 */
class ProductType extends CI_Controller
{

	function __construct()
	{
		parent::__construct();

		$this->load->model('producttype_model', 'productType');
		$this->load->model('product_model', 'product');
		$this->load->model('category_model', 'category');
	}

	public function index()
	{		
		if (Page::getInstance()->isAdmin())
		{			
			$categories = Category_Model::getInstance()->getStorageCategories();
			$producttypes = array();
			
			$id = $this->session->userdata('categoryid');
			
			if ($id == NULL)
			{
				$id = count($categories) > 0 ? $categories[0]->getId() : 0;
			}
			
			$producttypes = $this->productType->getProductTypesByCategoryId($id);
			
			$data['producttypes'] = $producttypes;
			$data['categories'] = $categories;
			
			$this->session->set_userdata('categoryid', $id);
			
			$this->load->view('producttypes/producttype_admin_view', $data);
		}
		else
		{
			redirect(site_url());
		}
	}
	
	public function viewByCategory($id)
	{
		$this->session->set_userdata('categoryid', $id);
		redirect('producttype');
	}
	
	public function sortview($categoryid)
	{
		$error = ERROR_NONE;
		
		if (Page::getInstance()->isAdmin())
		{
			$error = $this->session->set_userdata('sort', TRUE);
		}
		else
		{
			redirect(site_url());
		}
		
		$this->session->set_userdata('categoryid', $categoryid);
		$this->session->set_userdata('error', $error);
		redirect('producttype');
	}

	public function delete()
	{
		$error = ERROR_NONE;
	   
	   if (Page::getInstance()->isAdmin())
	   {
	   		$ids = $this->input->post('producttypes');
	   		$error = $this->productType->deleteMultipleProductTypes($ids);
	   }
		else
		{
			redirect(site_url());
		}
	   
	   $this->session->set_userdata('error', $error);
       redirect('producttype');
	}

	public function deleteProducts()
	{
		if (Page::getInstance()->isAdmin())
		{
			$error = ERROR_NONE;
			
			$productTypeId = $this->input->post('producttypeid');
			$productIds = $this->input->post('products');
			$error = $this->product_model->deleteMultipleProducts($productIds);
			
			$this->session->set_userdata('error', $error);
			redirect('producttype/view/'.$productIds);
		}
		else
		{
			redirect(site_url());
		}
	}
	
	public function addProducts($ptid, $curPage = 0)
	{
		if (Page::getInstance()->isAdmin())
		{
			$rec_limit = REC_LIMIT;
			
			if ($curPage == 0)
			{
				$offset = 0;
				$page = 0;
			}
			else {
				$page = $curPage - 1;
				$offset = $rec_limit * $page;
			}
			
			$total = $this->product->countProductsByProductTypeId($ptid, FALSE);
			$config = PaginationObject::getInstance()->createPaginationConfig('producttype/addproducts/'.$ptid, $total, $curPage, 4);
			
			$this->pagination->initialize($config);
			
			$data['paging_link'] = $this->pagination->create_links();
			$data['products'] = $this->product->getProductsByProductTypeId($ptid, $offset, $rec_limit, FALSE);
			$producttype = $this->producttype_model->getProductTypeById($ptid);
			$data['producttype'] = $producttype;
			
			if ($producttype->getId() <= 0)
				$data['rerror'] = NOT_EXISTED_ERROR;
			
			$this->load->view('producttypes/producttype_add_products_admin_view', $data);
		}
		else
		{
			redirect(site_url());
		}
	}
	
	public function processAddProducts()
	{
		if (Page::getInstance()->isAdmin())
		{
			$error = ERROR_NONE;
			
			$productTypeId = $this->input->post('producttypeid');
			$productIds = $this->input->post('products');
			
			$error = $this->product->updateProductType($productIds, $productTypeId);
			
			$this->session->set_userdata('error', $error);
			if ($error != ERROR_NONE)
				redirect('producttype/addproducts/'.$productTypeId);
			else
				redirect('producttype/view/'.$productTypeId);
		}
		else
		{
			redirect(site_url());
		}
	}
	
	public function insert()
	{
		$error = ERROR_NONE;
		if (Page::getInstance()->isAdmin())
		{
			$name = $this->input->post('producttypename');
			$categoryId = $this->input->post('category');
			
			if ($this->productType->findProductType($categoryId, $name) > 0)
			{
				$error = EXISTED_ERROR;
				$this->session->set_userdata('producttypename', $name);
			}
			else
			{
				$productType = new ProductTypeObject(0, $categoryId, $name);
				$error = $this->productType->insertProductType($productType);
			}
		}
		else
		{
			redirect(site_url());
		}
		
		$this->session->set_userdata('categoryid', $categoryId);
		$this->session->set_userdata('error', $error);
		redirect('producttype');
	}
	public function view($id, $curPage = 0)
	{
		$error = ERROR_NONE;
		
		$rec_limit = REC_LIMIT;
		
		if ($curPage == 0)
		{
			$offset = 0;
			$page = 0;
		}
		else {
			$page = $curPage - 1;
			$offset = $rec_limit * $page;
		}
		
		$productType = $this->productType->getProductTypeById($id);
		$category = $this->category->getCategoryById($productType->getCategoryId());
		$products = $this->product->getProductsByProductTypeId($id, $offset, $rec_limit);
		$total = $this->product->countProductsByProductTypeId($id);
		$config = PaginationObject::getInstance()->createPaginationConfig('producttype/view/'.$id, $total, $curPage, 4);
		
		$this->pagination->initialize($config);
		
		if ($productType->getId() <= 0)
			$data['rerror'] = NOT_EXISTED_ERROR;
		
		$data['paging_link'] = $this->pagination->create_links();
		$data['producttype'] = $productType;
		$data['category'] = $category;
		$data['products'] = $products;
		
		if (Page::getInstance()->isAdmin())
			$this->load->view('producttypes/producttype_products_admin_view', $data);
		else
			$this->load->view('producttype_product_view', $data);
	}
	
	
	public function edit($id)
	{
		$productype = $this->productType->getProductTypeById($id);
		
		if ($productype->getId() <= 0)
			$data['rerror'] = NOT_EXISTED_ERROR;
		
		$data['producttype'] = $productype;
		$this->load->view('producttypes/producttype_admin_detail', $data);
	}
	
	public function update()
	{
		$error = ERROR_NONE;
		$id = $this->input->post('producttypeid');
		$name = $this->input->post('producttypename');
		$categoryId = $this->input->post('categoryid');
		
		if (Page::getInstance()->isAdmin())
		{
			$productType = $this->productType->getProductTypeById($id);
			
			$foundId = $this->productType->findProductType($categoryId, $name);
			$existed = FALSE;
			
			if ($foundId == $id)
				$existed = FALSE;
			else {
				if ($foundId > 0)
					$existed = TRUE;
			}
		
			if (! $existed)
			{
				$productType->setTitle($name);
				$productType->setCategoryId($categoryId);
				$order = $this->productType->countProductTypeByCategoryId($categoryId);
				$productType->setOrder($order + 1);
				
				$error = $this->productType->updateProductType($id, $productType);
			}
			else {
				$error = EXISTED_ERROR;
				$this->session->set_userdata('producttypename', $name);
			}
		}
		else
		{
			redirect(site_url());
		}
		
		$this->session->set_userdata('error', $error);
		$this->session->set_userdata('categoryid', $categoryId);
		
		if ($error == ERROR_NONE)
			redirect('producttype');
		else
		{
			$url = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : site_url();
			redirect($url);
		}
	}
	
	public function saveSorting()
	{
		$error = ERROR_NONE;
		$categoryid = $this->input->post('categoryview');
		
		if (Page::getInstance()->isAdmin())
		{
			$ids = $this->input->post('ids');
			$orders = $this->input->post('orders');
			
			$error = $this->producttype_model->updateOrder($ids, $orders);
		}
		else
		{
			redirect(site_url());
		}
		
		$this->session->set_userdata('categoryid', $categoryid);
		$this->session->set_userdata('error', $error);
		redirect('producttype');
	}

}
/* End of file producttype.php */
/* Location: ./application/controllers/producttype.php */